package com.liuchuanzheng.lcz_wanandroid.module.home.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.base.BaseVMFragment
import com.liuchuanzheng.lcz_wanandroid.databinding.FragmentProjectTypeBinding
import com.liuchuanzheng.lcz_wanandroid.module.common.bus.CollectStatusEvent
import com.liuchuanzheng.lcz_wanandroid.module.detail.activity.DetailWebActivity
import com.liuchuanzheng.lcz_wanandroid.module.home.adapter.ProjectAdapter
import com.liuchuanzheng.lcz_wanandroid.module.home.adapter.CategoryAdapter
import com.liuchuanzheng.lcz_wanandroid.module.home.viewmodel.ProjectCategoryViewModel
import com.liuchuanzheng.lcz_wanandroid.module.user.UserManager
import com.liuchuanzheng.lcz_wanandroid.module.user.activity.LoginActivity
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

/**
 * @author 刘传政
 * @date 2021/9/14 0014 17:24
 * QQ:1052374416
 * 电话:18501231486
 * 作用:项目分类
 * 注意事项:
 */
class ProjectCategoryFragment: BaseVMFragment<ProjectCategoryViewModel>() {
    lateinit var mViewBinding:FragmentProjectTypeBinding
    val projectAdapter: ProjectAdapter by lazy { ProjectAdapter() }
    val categoryAdapter: CategoryAdapter by lazy { CategoryAdapter() }
    /**
     * 当页面可见的时候，才加载当前页面。 没有打开的页面，就不会预加载。
     *
     *
     * 说白了，懒加载就是可见的时候才去请求数据。
     * 只回调一次
     */
    override fun onLazyLoad() {
        mViewModel.getProjectCategories()
    }

    /**
     * 因为系统自带的设置view方法有很多种,id,view都可以.所以这里不像
     * 大家都用id的封装方式,那是限制了自己
     * @param inflater
     * @param container
     */
    override fun creatView(inflater: LayoutInflater?, container: ViewGroup?): View {
        mViewBinding = FragmentProjectTypeBinding.inflate(inflater!!, container, false)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewBinding.smartRefreshLayout.setEnableRefresh(true)
        mViewBinding.smartRefreshLayout.setEnableLoadMore(true)
        mViewBinding.smartRefreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                mViewModel.getProjectListByCid(true)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewModel.getProjectListByCid(false)
            }

        })
        initTypeAdapter()
        initArticleAdapter()
    }
    private fun initTypeAdapter() {

        categoryAdapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            categoryAdapter.check(i)
            mViewModel.checkCategory(i)
        }
        mViewBinding.rvCategory.adapter = categoryAdapter
    }
    private fun initArticleAdapter() {
        mViewBinding.recyclerView.setLayoutManager(LinearLayoutManager(activity))
        mViewBinding.recyclerView.adapter = projectAdapter
        projectAdapter.setOnItemClickListener { adapter, view, position ->
            DetailWebActivity.start(requireContext(),this@ProjectCategoryFragment.projectAdapter.getItem(position) )
        }
        projectAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.iv_collect) {
                if (UserManager.isLogin()) {
                    var article = this@ProjectCategoryFragment.projectAdapter.data[position]
                    if (article.collect) {
                        mViewModel.uncollect(article.id)
                    }else{
                        mViewModel.collect(article.id)
                    }
                }else{
                    LoginActivity.start(requireActivity())
                }

            }
        }
        LiveEventBus
            .get(CollectStatusEvent::class.java)
            .observe(this, Observer { event->
                val item = projectAdapter.data.find { it.id == event.id }
                if (item != null) {
                    item.collect = event.collect
                    projectAdapter.notifyDataSetChanged()
                }

            })
    }
    /**
     * 进行观察订阅
     */
    override fun observeViewModel() {
        mViewModel.typeList.observe(this,{
            mViewBinding.rvCategory.isGone = it.isEmpty()
            categoryAdapter.setList(it)
        })
        mViewModel.projectList.observe(this,{
            if (it.isRefresh) {
                //刷新的结果
                projectAdapter.setList(it.dataList)
                mViewBinding.recyclerView.scrollToPosition(0)

            }else{
                //加载更多的结果
                projectAdapter.addData(it.dataList!!)
            }
        })
        mViewModel.smartRefreshStatus.observe(this,{
            if (it) {
                //基本不会触发到
                mViewBinding.smartRefreshLayout.autoRefreshAnimationOnly()
            }else{
                mViewBinding.smartRefreshLayout.finishRefresh()
            }
        })
        mViewModel.smartLoaMoreStatus.observe(this,{
            if (it) {
                //基本不会触发到
                mViewBinding.smartRefreshLayout.autoLoadMoreAnimationOnly()
            }else{
                mViewBinding.smartRefreshLayout.finishLoadMore()
            }
        })
        mViewModel.liveData_loading.observe(this,{
            if (it) {
                showLoading("加载中")
            }else{
                hideLoading()
            }

        })
    }
}